package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/11/23.<br><br>
 *
 * 中等：外观数列
 */
public class Topic38 {

    public static void main(String[] args) {
        Topic38 instance = new Topic38();

        System.out.println(instance.countAndSay(1));
        System.out.println(instance.countAndSay(2));
        System.out.println(instance.countAndSay(3));
        System.out.println(instance.countAndSay(4));
        System.out.println(instance.countAndSay(5));

        System.out.println(instance.helper("3322251"));
    }

    public String countAndSay(int n) {
        if(n <= 1) {
            return "1";
        }

        String result = "1";
        for(int i = 1; i < n; i++) {
            result = helper(result);
        }

        return result;
    }

    private String helper(String init) {
        StringBuilder builder = new StringBuilder();

        int start = 0;
        int end = 1;
        char[] array = init.toCharArray();
        while (end < init.length()) {
            if(array[end] != array[start]) {
                builder.append(end - start).append(array[start]);
                start = end;
            }

            end++;
        }

        builder.append(end - start).append(array[start]);
        return builder.toString();
    }
}
